package cn.gpnu.dao;

import cn.gpnu.domain.User;
import cn.gpnu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

//操作数据库中USER表的类
public class Userdao {
    //声明JDBCTemplate对象共用

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource());


    public User login(User loginUser){
        try {
            //1编写sql
            String sql = "select * from user_forest where username_forest = ? and password_forest = ?";
            //2调用query方法
            User user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    loginUser.getUsername(),loginUser.getPassword());

            return user;
        } catch (DataAccessException e) {
           // e.printStackTrace();
            return null;
        }
    }
    public boolean Register(User Register){
        try {
            //1编写sql
            String sql = "INSERT INTO user_forest (name,username_forest,password_forest,gold,now_tree,today_study_time) VALUES (?,?,?,?,?,?);";
            //2调用query方法

            template.update(sql,Register.getName(),Register.getUsername(), Register.getPassword(),0,0,0);
//
            Userdao dao = new Userdao();
            User user = dao.login(Register);
            System.out.println(user);

            for( int i = 1 ; i < 10 ; i++){
                String sql2 = "INSERT INTO user_to_tree (tree_state,tree_id,user_id) VALUES (?,?,?);";
                template.update(sql2,0,i,user.getId());
            }

            return true;
        } catch (DataAccessException e) {
            // e.printStackTrace();
            System.out.println("失败了");
            return false;
        }
    }
    public boolean add_gold(int id,int gold){
        try {
            //1编写sql
            String sql = "UPDATE user_forest SET gold = gold + ? WHERE id = ?";
            //2调用query方法
            template.update(sql,gold,id);
            return true;
        } catch (DataAccessException e) {

            System.out.println("添加金币失败");
            return false;
        }
    }

    public User select_user(int id){
        try {
            //1编写sql
            String sql = "select * from user_forest where id = ?";
            //2调用query方法
            User user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    id);

            return user;
        } catch (DataAccessException e) {
            // e.printStackTrace();
            return null;
        }
    }

    public void buy_tree(int id,int tree_price){
        try {
            //1编写sql
            String sql = "UPDATE user_forest SET gold = gold-? WHERE id = ?;";
            //2调用query方法
            template.update(sql,tree_price,id);
        } catch (DataAccessException e) {
             e.printStackTrace();
        }
    }

    public void sell_tree(int id,int treeprice){
        try {
            //1编写sql
            String sql = "UPDATE user_forest SET gold = gold + ? WHERE id = ?;";
            //2调用query方法
            template.update(sql,treeprice/2,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    public void choose_tree(int user_id,int tree_id){
        try {
            //1编写sql
            String sql = "UPDATE user_forest SET now_tree = ? WHERE id = ?;";
            //2调用query方法
            template.update(sql,tree_id,user_id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    public void add_time(int user_id,int time){
        try {
            //1编写sql
            String sql = "UPDATE user_forest SET today_study_time = today_study_time + ? WHERE id = ?;";
            //2调用query方法
            template.update(sql,time,user_id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
}


